﻿
//Author: 张亚涛
//createtime:2011-10-13 10:35:35
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using NS.DAL.Systems;


namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "CouponsScheme" 表的方法;
    /// </summary>
    public partial class CouponsSchemeBase
    {
        const string SQL_Select = "select [SchemeID],[CouponName],[Quantity],[OldPrice],[PrePrice],[ValidDays],[Scope],[Explain] from [CouponsScheme] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回CouponsScheme表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<CouponsScheme> GetAllCouponsScheme()
        {
            List<CouponsScheme> couponsSchemeList = new List<CouponsScheme>();
            SqlDataReader reader = null;

            string sql = SQL_Select + " order by PrePrice";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                CouponsScheme couponsScheme = new CouponsScheme(reader);
                couponsSchemeList.Add(couponsScheme);
            }
            reader.Close();
            return couponsSchemeList;
        }
        /// <summary>
        /// 根据CouponsScheme表的SchemeID字段返回数据 
        /// </summary>
        /// <param name="schemeID">SchemeID</param>
        /// <returns></returns>       
        public static CouponsScheme GetCouponsSchemeBySchemeID(int schemeID)
        {
            CouponsScheme couponsScheme = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [SchemeID] = @SchemeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@SchemeID",schemeID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                couponsScheme = new CouponsScheme(reader);
            }
            reader.Close();
            return couponsScheme;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据CouponsScheme表的SchemeID字段删除数据 
        /// </summary>
        /// <param name="schemeID">schemeID</param>
        /// <returns></returns>        
        public static int DeleteCouponsSchemeBySchemeID(int schemeID)
        {
            string sql = "delete from [CouponsScheme] where [SchemeID] = @SchemeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@SchemeID",schemeID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(CouponsScheme couponsScheme)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@SchemeID",couponsScheme.SchemeID),
            new SqlParameter("@CouponName",couponsScheme.CouponName),
            new SqlParameter("@Quantity",couponsScheme.Quantity),
            new SqlParameter("@OldPrice",couponsScheme.OldPrice),
            new SqlParameter("@PrePrice",couponsScheme.PrePrice),
            new SqlParameter("@ValidDays",couponsScheme.ValidDays),
            new SqlParameter("@Scope",couponsScheme.Scope),
            new SqlParameter("@Explain",couponsScheme.Explain)    
            };
            return paras;
        }
        /// <summary>
        /// 根据CouponsScheme表的SchemeID字段更新数据 
        /// </summary> 
        /// <param name="CouponsScheme">couponsScheme</param>
        /// <returns></returns>       
        public static int UpdateCouponsSchemeBySchemeID(CouponsScheme couponsScheme)
        {
            string sql = "update [CouponsScheme] set [CouponName] = @CouponName,[Quantity] = @Quantity,[OldPrice] = @OldPrice,[PrePrice] = @PrePrice,[ValidDays] = @ValidDays,[Scope] = @Scope,[Explain] = @Explain where [SchemeID] = @SchemeID";
            SqlParameter[] paras = PrepareCommandParameters(couponsScheme);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向CouponsScheme表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="couponsScheme">CouponsScheme</param>       
        /// <returns></returns>        
        public static int AddCouponsScheme(CouponsScheme couponsScheme)
        {
            string sql = "insert into [CouponsScheme]([CouponName],[Quantity],[OldPrice],[PrePrice],[ValidDays],[Scope],[Explain])values(@CouponName,@Quantity,@OldPrice,@PrePrice,@ValidDays,@Scope,@Explain)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(couponsScheme);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回CouponsScheme表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllCouponsScheme_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}
